/*     */ package com.zimbra.cs.account;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import java.security.SecureRandom;
/*     */ import java.util.HashMap;
/*     */ import org.apache.commons.codec.DecoderException;
/*     */ import org.apache.commons.codec.binary.Hex;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ExtAuthTokenKey
/*     */ {
/*     */   public static final int KEY_SIZE_BYTES = 32;
/*     */   private byte[] key;
/*     */   private long version;
/*     */   private long created;
/*  39 */   private static HashMap<String, ExtAuthTokenKey> keyCache = new HashMap();
/*     */   private static ExtAuthTokenKey latestExtAuthKey;
/*     */   
/*     */   public byte[] getKey() {
/*  43 */     return this.key;
/*     */   }
/*     */   
/*     */   public long getVersion() {
/*  47 */     return this.version;
/*     */   }
/*     */   
/*     */   public long getCreated() {
/*  51 */     return this.created;
/*     */   }
/*     */   
/*     */   void setKey(byte[] key) {
/*  55 */     this.key = key;
/*     */   }
/*     */   
/*     */   ExtAuthTokenKey(long version, byte[] key) throws ServiceException {
/*  59 */     this.version = version;
/*  60 */     this.created = System.currentTimeMillis();
/*  61 */     if (key != null) {
/*  62 */       this.key = key;
/*     */     } else {
/*  64 */       SecureRandom random = new SecureRandom();
/*  65 */       this.key = new byte[32];
/*  66 */       random.nextBytes(this.key);
/*     */     }
/*     */   }
/*     */   
/*     */   private ExtAuthTokenKey(String k) throws ServiceException {
/*  71 */     String[] parts = k.split(":");
/*  72 */     if (parts.length != 3)
/*  73 */       throw ServiceException.INVALID_REQUEST("invalid auth token key", null);
/*  74 */     String ver = parts[0];
/*  75 */     String created = parts[1];
/*  76 */     String data = parts[2];
/*     */     try
/*     */     {
/*  79 */       this.version = Long.parseLong(ver);
/*     */     } catch (NumberFormatException e) {
/*  81 */       throw ServiceException.INVALID_REQUEST("invalid auth token key version", e);
/*     */     }
/*     */     try
/*     */     {
/*  85 */       this.created = Long.parseLong(created);
/*     */     } catch (NumberFormatException e) {
/*  87 */       throw ServiceException.INVALID_REQUEST("invalid auth token key created data", e);
/*     */     }
/*     */     try
/*     */     {
/*  91 */       this.key = Hex.decodeHex(data.toCharArray());
/*     */     } catch (DecoderException e) {
/*  93 */       throw ServiceException.INVALID_REQUEST("invalid auth token key data", e);
/*     */     }
/*     */   }
/*     */   
/*     */   public String getEncoded() {
/*  98 */     return this.version + ":" + this.created + ":" + new String(Hex.encodeHex(this.key));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static ExtAuthTokenKey getVersion(String version)
/*     */     throws ServiceException
/*     */   {
/* 108 */     ExtAuthTokenKey key = (ExtAuthTokenKey)keyCache.get(version);
/*     */     
/*     */ 
/* 111 */     if (key == null) {
/* 112 */       refresh(false);
/*     */     }
/* 114 */     key = (ExtAuthTokenKey)keyCache.get(version);
/*     */     
/*     */ 
/* 117 */     if (key == null)
/* 118 */       refresh(true);
/* 119 */     key = (ExtAuthTokenKey)keyCache.get(version);
/*     */     
/*     */ 
/* 122 */     return key;
/*     */   }
/*     */   
/*     */   private static synchronized void refresh(boolean reload) throws ServiceException {
/* 126 */     Provisioning prov = Provisioning.getInstance();
/* 127 */     Config config = prov.getConfig();
/*     */     
/* 129 */     if (reload) {
/* 130 */       prov.reload(config);
/*     */     }
/* 132 */     String key = config.getAttr("zimbraExternalAccountProvisioningKey");
/*     */     
/* 134 */     if (StringUtil.isNullOrEmpty(key)) {
/* 135 */       prov.reload(config);
/* 136 */       key = config.getAttr("zimbraExternalAccountProvisioningKey");
/*     */     }
/*     */     
/*     */ 
/* 140 */     if (StringUtil.isNullOrEmpty(key)) {
/* 141 */       ExtAuthTokenKey extAuthkey = new ExtAuthTokenKey(0L, null);
/* 142 */       HashMap<String, String> attrs = new HashMap();
/* 143 */       attrs.put("zimbraExternalAccountProvisioningKey", extAuthkey.getEncoded());
/* 144 */       Provisioning.getInstance().modifyAttrs(config, attrs);
/* 145 */       key = config.getAttr("zimbraExternalAccountProvisioningKey");
/*     */     }
/*     */     
/* 148 */     ExtAuthTokenKey extAuthKey = (ExtAuthTokenKey)keyCache.get(key);
/* 149 */     if (extAuthKey == null) {
/* 150 */       extAuthKey = new ExtAuthTokenKey(key);
/* 151 */       keyCache.put(key, extAuthKey);
/* 152 */       keyCache.put(Long.toString(extAuthKey.version), extAuthKey);
/* 153 */       if ((latestExtAuthKey == null) || (latestExtAuthKey.version < latestExtAuthKey.version))
/* 154 */         latestExtAuthKey = extAuthKey;
/*     */     }
/*     */   }
/*     */   
/*     */   public static synchronized ExtAuthTokenKey getCurrentKey() throws ServiceException {
/* 159 */     if (latestExtAuthKey == null) {
/* 160 */       refresh(false);
/*     */     }
/* 162 */     return latestExtAuthKey;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/ExtAuthTokenKey.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */